package com.example.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TranslateController {

    private static final String TRANSLATE_URL = "http://139.9.185.3:8080/translate-message";

    private static final Map<String, String> languageMap = new HashMap<>();

    static {
        // 将中文语言列表添加到哈希表中
        languageMap.put("中文", "zh");
        languageMap.put("英语", "en");
        languageMap.put("粤语", "yue");
        languageMap.put("文言文", "wyw");
        languageMap.put("日语", "jp");
        languageMap.put("韩语", "kor");
        languageMap.put("法语", "fra");
        languageMap.put("西班牙语", "spa");
        languageMap.put("泰语", "th");
        languageMap.put("阿拉伯语", "ara");
        languageMap.put("俄语", "ru");
        languageMap.put("葡萄牙语", "pt");
        languageMap.put("德语", "de");
        languageMap.put("意大利语", "it");
        languageMap.put("希腊语", "el");
        languageMap.put("荷兰语", "nl");
        languageMap.put("波兰语", "pl");
        languageMap.put("保加利亚语", "bul");
        languageMap.put("爱沙尼亚语", "est");
        languageMap.put("丹麦语", "dan");
        languageMap.put("芬兰语", "fin");
        languageMap.put("捷克语", "cs");
        languageMap.put("罗马尼亚语", "rom");
        languageMap.put("斯洛文尼亚语", "slo");
        languageMap.put("瑞典语", "swe");
        languageMap.put("匈牙利语", "hu");
        languageMap.put("繁体中文", "cht");
        languageMap.put("越南语", "vie");
    }
    public void translate() {
        // 创建GridPane布局
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(20, 20, 20, 20));
        grid.setVgap(8);
        grid.setHgap(10);

        // 创建文本框和标签
        TextField messageInput = new TextField();
        messageInput.setPromptText("Enter message to translate");
        GridPane.setConstraints(messageInput, 0, 0);

        ComboBox<String> languageDropdown = new ComboBox<>();
        languageDropdown.getItems().addAll(languageMap.keySet());
        languageDropdown.setValue("英文"); // 默认选择英文
        GridPane.setConstraints(languageDropdown, 1, 0);

        Button translateButton = new Button("翻译");
        GridPane.setConstraints(translateButton, 2, 0);

        TextArea resultArea = new TextArea();
        resultArea.setPrefSize(300, 200);
        resultArea.setEditable(false);
        resultArea.setWrapText(true); // 设置自动换行
        GridPane.setConstraints(resultArea, 0, 1, 3, 1);

        // 添加控件到GridPane中
        grid.getChildren().addAll(messageInput, languageDropdown, translateButton, resultArea);

        // 创建弹窗
        Dialog<String> dialog = new Dialog<>();
        dialog.setTitle("Translation Dialog");
        dialog.setHeaderText("即时翻译");
        dialog.getDialogPane().setContent(grid);

        // 添加按钮
        ButtonType confirmButtonType = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(confirmButtonType, ButtonType.CANCEL);

        // 翻译按钮事件处理程序
        translateButton.setOnAction(event -> {
            // 这里执行翻译操作并更新resultArea
            String message = messageInput.getText();
            String targetLanguage = languageMap.get(languageDropdown.getValue()); // 获取选中语言的英文缩写
            if (targetLanguage == null){
                targetLanguage = "en";
            }
            String translationResult = translateMessage(message, targetLanguage); // 调用翻译接口
            if (translationResult != null) {
                resultArea.setText(translationResult);
            }
        });

        // 显示窗口
        dialog.showAndWait();
    }



    // 模拟调用翻译接口
    private String translateMessage(String message, String targetLanguage) {
        // 创建HTTP客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建HttpPost请求
        HttpPost httpPost = new HttpPost(TRANSLATE_URL);
        // 创建翻译消息对象
        String jsonBody = "{" + "\"message\":\"" + message + "\",\"targetLanguage\":\"" + targetLanguage + "\"}";
        // 设置请求体
        httpPost.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));
        // 发送请求并获取响应
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            // 解析响应
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String jsonResponse = EntityUtils.toString(responseEntity);
                // 解析JSON响应
                ObjectMapper objectMapper = new ObjectMapper();
                TranslateResponse translateResponse = objectMapper.readValue(jsonResponse, TranslateResponse.class);
                // 使用解析后的数据
                int errno = translateResponse.getErrno();
                if(errno == 0){
                    return translateResponse.getData().getResult();
                }
            } else {
                return "Empty response received from the translation server";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to send translation request: " + e.getMessage();
        }
        return null;
    }
}

